<?php

namespace app\stores\controller\miniapp\promotion;

use app\BaseController;
use app\stores\model\promotion\ActivityModel;
use app\stores\model\promotion\TeamActivityModel;

class SelectCenter extends BaseController{

    /*选品中心，只有团长有*/
    public function getList(){
        $page=$this->request->page;
        $limit=$this->request->limit;

        //获取当前用户(团长)所属团队
        $team_id = $this->request->teamId;
        $is_lead = $this->request->isLead;

        if($is_lead<1) return $this->error('团长才可以查看');

        //排除已选活动
        $not_activity_ids = TeamActivityModel::getId(['team_id'=>$team_id,'is_delete'=>0]);

        //不指定团队和指定该团队
        $activity_ids = ActivityModel::getId("a.type=1 and a.status=1  and ((a.is_appoint_team=0) or (a.is_appoint_team=1 and FIND_IN_SET('{$team_id}',a.team_ids)))");

        $where['status'] = 1;
        $where['type'] = 1;
        $where['activity_ids'] = $activity_ids;
        $where['not_activity_ids'] = $not_activity_ids;
        $order = ['a.add_time'=>'desc'];
        $field = 'a.*,g.sell_time_type,g.sell_start_time,g.sell_end_time,g.cover_img,g.name,g.original_price,g.total_value,g.type,sd.t_lng,sd.t_lat,sd.logo,sd.tel,sd.per_money,sd.address,s.name';
        $return = ActivityModel::list($where,$order,$field,$page,$limit);
        return $this->success($return);
    }
    public function detail(){
        $team_id = $this->request->teamId;
        $activity_id = $this->request->param('activity_id/d',0);
        if($activity_id<1) return $this->error('活动不存在');

        $field = "a.*,g.cover_img,g.sell_time_type,g.sell_start_time,g.sell_end_time,g.goods_details,g.purchase_information,g.use_limit,g.use_limit_detail,g.type,g.original_price,g.crossed_price,g.total_value,g.type,s.name as shop_name,sd.logo,sd.per_money,sd.t_lng,sd.t_lat,sd.tel,sd.address";
        $activity= ActivityModel::info(['a.activity_id'=>$activity_id,'a.status'=>1,'a.type'=>1,'g.is_delete'=>0],$field);
        if($activity===null) return $this->error('活动不存在');

        if($activity['is_appoint_team']>0){
            if(empty($activity['team_ids'])) return $this->error('没有权限查看该活动');
            if(!in_array($team_id,$activity['team_ids'])) return $this->error('没有权限查看该活动');
        }
        return $this->success($activity);
    }
    /*添加到我的推广*/
    public function addTeamActivity(){
        $is_lead = $this->request->isLead;
        $team_id = $this->request->teamId;
        $activity_id = $this->request->param('activity_id/d',0);
        $team_commission = $this->request->param('team_commission/f',0);

        if($is_lead<1) return $this->error('团长才可以添加');
        if($activity_id<1) return $this->error('活动不存在');

        $activity= ActivityModel::info(['a.activity_id'=>$activity_id,'a.status'=>1,'a.type'=>1,'g.is_delete'=>0]);
        if($activity===null) return $this->error('活动不存在');

        $t_activity_id = TeamActivityModel::getId(['activity_id'=>$activity_id,'team_id'=>$team_id,'is_delete'=>0]);
        if(!empty($t_activity_id)) return $this->error('请勿重复添加');

        $t_activity = new TeamActivityModel();
        $t_activity->shop_id = $activity['shop_id'];
        $t_activity->team_id = $team_id;
        $t_activity->activity_id = $activity_id;
        $t_activity->team_commission = $team_commission;
        $t_activity->save();

        return $this->success('添加成功');
    }
}

